package com.hxzy.wx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.hxzy.common.util.R;
import com.hxzy.drvier.api.DriverOpenFeignApi;
import com.hxzy.drvier.req.CreateDriverFaceModelReq;
import com.hxzy.drvier.req.RegisterNewDriverReq;
import com.hxzy.drvier.req.UpdateDriverAuthReq;
import com.hxzy.drvier.resp.DriverResp;
import com.hxzy.wx.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wy
 * @Description 微信端司机API
 * @Date 2023/12/14 10:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/driver")
@Tag(name = "WxDriverController", description = "微信端司机模块Web接口")
public class WxDriverController {

    @Autowired
    private DriverService  driverService;
    

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R<Map<String,String>>  registerNewDriver(@RequestBody @Valid RegisterNewDriverReq req){
        String token = this.driverService.registerNewDriver(req);

        Map<String,String> map=new HashMap<>();
        map.put("token", token);

        return R.ok(map);
    }

    /**
     * 如何这里采用注解，令牌过期这种情况，一定会报NotLoginException，不要使用 @SaCheckLogin
     * @return
     */
    @Operation(summary = "新司机注册检查认证状态")
    @GetMapping(value = "/registerRealAuth")
    public R<Integer> registerRealAuth(){
        //判断令牌是否有效(登录判断)
        boolean isLogin = StpUtil.isLogin();
        if(isLogin){
            //获取令牌对应的userId的值
          long userId=StpUtil.getLoginIdAsLong();
          DriverResp driverResp=this.driverService.getDriverInfo(userId);

          if(driverResp.getRealAuth()==1){
              //要跳转到 上传身份证的界面
              return R.ok(1);
          }else if (driverResp.getArchive()==0){
             //上传过照片，但是没有人脸认证
              return R.ok(3);
          }else{
              //已经上传过了，但是还没有申核通过
              return R.ok(2);
          }
        }else{
            //令牌过期，重新申请获取令牌
            return  R.ok(3);
        }
    }


    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新实名认证信息")
    @SaCheckLogin
    public R updateDriverAuth(@RequestBody @Valid UpdateDriverAuthReq req) {
        long driverId = StpUtil.getLoginIdAsLong();
        req.setDriverId(driverId);

        int rows = driverService.updateDriverAuth(req);

        Map<String,Integer>  dataMap=new HashMap<>();
        dataMap.put("rows",rows);
        return R.ok(dataMap);
    }

    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建司机人脸模型归档")
    @SaCheckLogin
    public R createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelReq req) {
        long driverId = StpUtil.getLoginIdAsLong();
        req.setDriverId(driverId);

        R<Map<String, String>>  result = driverService.createDriverFaceModel(req);
        return result;
    }

}
